package com.caojing.haolemai.ui.homedetail;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.bean.ContentBean;
import com.caojing.haolemai.config.UrlConfig;
import com.caojing.haolemai.ui.adapter.HomeDetailGvAdapter;
import com.caojing.haolemai.ui.product.ProductActivity;
import com.caojing.haolemai.view.MyGridView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class HomeDetailActivity extends BaseActivity implements HomeDetailContract.View {
    @Bind(R.id.home_detail_ivBack)
    ImageView homeDetailIvBack;
    @Bind(R.id.home_detail_tvTitle)
    TextView homeDetailTvTitle;
    @Bind(R.id.home_detail_ivShare)
    ImageView homeDetailIvShare;
    @Bind(R.id.home_detail_tvSort)
    TextView homeDetailTvSort;
    @Bind(R.id.home_detail_tvDefault)
    TextView homeDetailTvDefault;
    @Bind(R.id.home_detail_tvSale)
    TextView homeDetailTvSale;
    @Bind(R.id.home_detail_tvPrice)
    TextView homeDetailTvPrice;
    @Bind(R.id.home_detail_tvDiscount)
    TextView homeDetailTvDiscount;
    @Bind(R.id.home_detail_ivHead)
    ImageView homeDetailIvHead;
    @Bind(R.id.home_detail_Gv)
    MyGridView homeDetailGv;
    @Bind(R.id.home_detail_sv)
    PullToRefreshScrollView homeDetailSv;
    private HomeDetailContract.presenter presenter;
    private HomeDetailGvAdapter adapter;
    private List<ContentBean.ResultsBean.ResultsBean2> totallist;
    private String contentId;
    /*初始化标志位*/
    private int index1 = 0,index2 = 0;
    /*默认页数*/
    private int PAGE_ID = 1;
    /*默认排序参数*/
    private int ORDER_BY =6;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_home_detail;
    }

    @Override
    protected void initView() {
        presenter = new HomeDetailPresenter(this);
        homeDetailGv.setFocusable(false);
        homeDetailTvDefault.setTextColor(Color.rgb(215,0,87));
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();

        contentId = intent.getStringExtra("contentId");
        presenter.initData(contentId, initParams());
        //Log.i("TAG",contentId);
        String name = intent.getStringExtra("name");
        if (!TextUtils.isEmpty(name)){
            homeDetailTvTitle.setText(name);
        }

        String ivurl = intent.getStringExtra("imageurl");
        Glide.with(this).load(ivurl).thumbnail(0.1f).centerCrop().placeholder(R.mipmap.global_default_image_outlets).error(R.mipmap.global_default_image_outlets).into(homeDetailIvHead);

        totallist = new ArrayList<>();
        adapter = new HomeDetailGvAdapter(totallist,this);
        homeDetailGv.setAdapter(adapter);

        homeDetailSv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                homeDetailSv.onRefreshComplete();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                PAGE_ID++;
                presenter.initData(contentId,initParams());
            }
        });

    }

    private HashMap<String, String> initParams() {
        HashMap<String, String> map = new HashMap<>();
        map.put(UrlConfig.Key.PAGE_SIZE, UrlConfig.DefaultValue.PAGE_SIZE);
        map.put(UrlConfig.Key.PAGE_ID, PAGE_ID + "");
        map.put(UrlConfig.Key.INSTOCK, UrlConfig.DefaultValue.INSTOCK);
        map.put(UrlConfig.Key.ORDER_BY, ORDER_BY + "");
        return map;
    }

    @Override
    public void onSuccessGetContentBean(ContentBean contentBean) {
        if (contentBean.getResults()!=null){
            totallist.addAll(contentBean.getResults().getResults());
        }
        //Log.i("TAG",""+contentBean.getResults().getResults().size());
        adapter.notifyDataSetChanged();
        homeDetailSv.onRefreshComplete();
        homeDetailGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String id = totallist.get(i).getProductId();
                Intent intent = new Intent(HomeDetailActivity.this, ProductActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onFailGetContentBean(String msg) {
        Toast.makeText(HomeDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.home_detail_tvDefault, R.id.home_detail_tvSale, R.id.home_detail_tvPrice, R.id.home_detail_tvDiscount})
    public void onClick(View view) {
        showColor();
        PAGE_ID=1;
        totallist.clear();
        switch (view.getId()) {
            case R.id.home_detail_tvDefault:
                homeDetailTvDefault.setTextColor(Color.rgb(215,0,87));
                index1=0;
                index2=0;
                ORDER_BY=6;
                break;
            case R.id.home_detail_tvSale:
                index1=0;
                index2=0;
                homeDetailTvSale.setTextColor(Color.rgb(215,0,87));
                ORDER_BY=0;
                break;
            case R.id.home_detail_tvPrice:
                homeDetailTvPrice.setTextColor(Color.rgb(215,0,87));
                if (index1%2==0){
                    /*Component组件，Compound复合，合成*/
                    Drawable drawable = getResources().getDrawable(R.mipmap.up_red);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
                    ORDER_BY=1;
                }else{
                    Drawable drawable = getResources().getDrawable(R.mipmap.up_red_down);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
                    ORDER_BY=2;
                }
                index1++;
                break;
            case R.id.home_detail_tvDiscount:
                homeDetailTvDiscount.setTextColor(Color.rgb(215,0,87));
                if (index2%2==0){

                    Drawable drawable1 = getResources().getDrawable(R.mipmap.up_red);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    homeDetailTvDiscount.setCompoundDrawables(null, null, drawable1, null);
                    ORDER_BY=4;
                }else{
                    Drawable drawable1 = getResources().getDrawable(R.mipmap.up_red_down);
                    drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                    homeDetailTvDiscount.setCompoundDrawables(null, null, drawable1, null);
                    ORDER_BY=5;
                }
                index2++;
                break;
        }
        presenter.initData(contentId,initParams());
    }
    @OnClick({R.id.home_detail_ivBack, R.id.home_detail_ivShare, R.id.home_detail_tvSort})
    public void onClick1(View view) {
        switch (view.getId()) {
            case R.id.home_detail_ivBack:
                finish();
                break;
            case R.id.home_detail_ivShare:
                break;
            case R.id.home_detail_tvSort:
                break;
        }
    }
    private void showColor() {
        homeDetailTvDefault.setTextColor(Color.BLACK);
        homeDetailTvSale.setTextColor(Color.BLACK);
        homeDetailTvPrice.setTextColor(Color.BLACK);
        homeDetailTvDiscount.setTextColor(Color.BLACK);
        Drawable drawable = getResources().getDrawable(R.mipmap.up_gray);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        homeDetailTvPrice.setCompoundDrawables(null, null, drawable, null);
        homeDetailTvDiscount.setCompoundDrawables(null, null, drawable, null);
    }
}
